package handlers

import (
	"net/http"

	"github.com/gin-gonic/gin"

	"pool/pool_backend/internal/service"
	"pool/pool_backend/pkg/logger"
)

// ClaimHandler Claim处理器
type ClaimHandler struct {
	claimService service.ClaimService
	chainService service.ChainService
}

// NewClaimHandler 创建Claim处理器
func NewClaimHandler(claimService service.ClaimService) *ClaimHandler {
	return &ClaimHandler{
		claimService: claimService,
	}
}

// GetClaimRecords 获取用户的Claim记录
func (h *ClaimHandler) GetClaimRecords(c *gin.Context) {
	// 从路径参数中获取地址
	address := c.Param("address")
	if address == "" {
		c.JSON(http.StatusBadRequest, gin.H{
			"code":    http.StatusBadRequest,
			"message": "Address is required",
		})
		return
	}

	// 获取Claim记录
	records, err := h.claimService.GetClaimRecords(address)
	if err != nil {
		logger.ErrorF("Failed to get claim records: %v", err)
		c.JSON(http.StatusInternalServerError, gin.H{
			"code":    http.StatusInternalServerError,
			"message": "Failed to get claim records: " + err.Error(),
		})
		return
	}

	c.JSON(http.StatusOK, gin.H{
		"code":    http.StatusOK,
		"message": "get record successful",
		"data":    records,
	})
}
// Claim 用户chaim积累的奖励
func (h *ClaimHandler) Claim(c *gin.Context){
	// 获取用户应该claim的奖励
	// 从路径参数中获取地址
	address := c.Param("address")
	if address == "" {
		c.JSON(http.StatusBadRequest, gin.H{
			"code":    http.StatusBadRequest,
			"message": "Address is required",
		})
		return
	}

	// 获取用户可claim的奖励数量
	amount, err := h.claimService.GetClaimableAmount(address)
	if err != nil {
		logger.ErrorF("Failed to get claimable amount: %v", err)
		c.JSON(http.StatusInternalServerError, gin.H{
			"code":    http.StatusInternalServerError,
			"message": "Failed to get claimable amount: " + err.Error(),
		})
		return
	}

	//	将制定数量的token转帐到用户的账户
	hash,err := h.chainService.Transfer(address,amount)
	if err != nil{
		logger.ErrorF("Failed to transfer token")
		c.JSON(http.StatusInternalServerError, gin.H{
			"code":    http.StatusInternalServerError,
			"message": "Failed to transfer token " + err.Error(),
		})
	}
	
	_,err = h.claimService.ClaimLevelToken(address,hash)
	if err != nil {
		logger.ErrorF("Failed to claim level token: %v", err)
		c.JSON(http.StatusInternalServerError, gin.H{
			"code":    http.StatusInternalServerError,
			"message": "Failed to claim level token: " + err.Error(),
		})
	}
	c.JSON(http.StatusOK, gin.H{
		"code":    http.StatusOK,
		"message": "get claimable amount successful",
		"data": gin.H{
			"amount": amount,
		},
	})
	

}

// RecordCheckIn 记录用户Check-In
func (h *ClaimHandler) RecordCheckIn(c *gin.Context) {
	// 从路径参数中获取地址
	address := c.Param("address")
	if address == "" {
		c.JSON(http.StatusBadRequest, gin.H{
			"code":    http.StatusBadRequest,
			"message": "Address is required",
		})
		return
	}

	// 获取请求参数
	var req struct {
		Hash string `json:"hash" binding:"required"`
	}

	if err := c.ShouldBindJSON(&req); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{
			"code":    http.StatusBadRequest,
			"message": "Invalid request: " + err.Error(),
		})
		return
	}

	// 验证交易hash是否正确
	ok,err := h.chainService.VerifyCheckinHash(address,req.Hash)
	if err != nil {
		logger.ErrorF("Failed to verify checkin hash: %v", err)
	}
	if !ok {
		c.JSON(http.StatusBadRequest, gin.H{
			"code":    http.StatusBadRequest,
			"message": "wrong hash: " + err.Error(),
		})
		return
	}

	// 记录Check-In
	err = h.claimService.RecordCheckIn(address, req.Hash)
	if err != nil {
		logger.ErrorF("Failed to record check-in: %v", err)
		c.JSON(http.StatusInternalServerError, gin.H{
			"code":    http.StatusInternalServerError,
			"message": "Failed to record check-in: " + err.Error(),
		})
		return
	}

	c.JSON(http.StatusOK, gin.H{
		"code":    http.StatusOK,
		"message": "check-in successful",
	})
} 
